package com.hancai.barrage.web.controller;

import com.hancai.barrage.model.JsonResult;
import com.hancai.barrage.service.SysEmployeeService;
import com.hancai.barrage.utils.TokenManager;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 涵菜
 * @version 1.0
 */
@RestController
public class LoginController {

    private final SysEmployeeService employeeService;

    public LoginController(SysEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @RequestMapping("/login")
    public JsonResult<String>login(String username,String password){
        try {
            String token=employeeService.login(username,password);
            return JsonResult.success(token);
        }catch (Exception e){
            return JsonResult.failed(400,e.getMessage());
        }
    }
    @RequestMapping("/logout")
    public JsonResult<?>logout(@RequestHeader(value=TokenManager.TOKEN_NAME,required = false)String token){
        TokenManager.removeInfo(token);
        return JsonResult.success();
    }

}
